﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Expo.Training.Entity;
using System.IO;
using Expo.Training.BRule;

public partial class Ad_EditAds : PageBase
{
    protected int adid;
    protected void Page_Load(object sender, EventArgs e)
    {
        adid = GetIntValue("id");
        if (!IsPostBack)
        {
            BindData();
        }
    }

    private void BindData()
    {
        Advertisement objAds = new Advertisement();
        objAds.ADID = adid;
        if (objAds.Exist())
        {
            this.txt_Title.Text = objAds.Title;
            this.ddlPosition.SelectedValue = objAds.Position;
            this.txt_url.Text = objAds.Url;
            this.ddlVesion.SelectedValue = objAds.Vesion;
            this.ImageFile.ImageUrl = ResolveClientUrl("~/Data/Ads/")+objAds.PictureUrl;
        }
        objAds.Dispose();
    }

    public void DoAdd(object sender, System.EventArgs e)
    {
        #region [ 添加新闻 ]

        string title = HttpUtility.HtmlEncode(this.txt_Title.Text.Trim());
        string url = HttpUtility.HtmlEncode(this.txt_url.Text.Trim());

        if (title.Length == 0)
        {
            sp_title_error.InnerText = "标题不能为空！";
            return;
        }
        Advertisement objAds = new Advertisement();
        objAds.ADID = adid;
        if (objAds.Exist())
        {
            objAds.Title = title;
            objAds.Url = url;
            Guid user = this.Member.MemberID;
            objAds.CreatorID = user;
            objAds.Position = this.ddlPosition.SelectedValue;
            objAds.Vesion = this.ddlVesion.SelectedValue;
            objAds.CreateDate = DateTime.Now;
            string folderName = ResolveClientUrl("~/Data/Ads/");
            if (!Directory.Exists(Server.MapPath(folderName)))
            {
                Directory.CreateDirectory(Server.MapPath(folderName));
            }
            string fileName = DateTime.Now.ToString("yyyyMMddHHmmssfff");
            string fileExtension = Path.GetExtension(this.File1.PostedFile.FileName).ToUpper();
            string filePath = Server.MapPath(folderName + fileName + fileExtension);
            if (fileExtension != "" || fileExtension == null)
            {
                if (!(fileExtension == ".JPEG" || fileExtension == ".JPG" || fileExtension == ".PNG" || fileExtension == ".GIF"))
                {
                    this.AddEndScript("error", "alert('不支持的图片格式！');");
                    return;
                }
                this.File1.PostedFile.SaveAs(filePath);
                CutImage image = new CutImage();
                image.CutImageBySize(filePath, Server.MapPath(folderName + fileName +"_n"+  fileExtension), 152, 44);
                if (File.Exists(filePath))
                {
                    File.Delete(filePath);
                }
                objAds.PictureUrl = fileName + "_n"+ fileExtension;
            }
            objAds.Tag = 1;
            if (objAds.Update())
            {
                Expo.Training.BRule.Audit.Audit.AuditLog(this.Member.MemberID, "友情链接管理 - 添加 - ID:" + objAds.ADID);
                Response.Redirect("Default.aspx");
            }
        }
        objAds.Dispose();

        #endregion
    }
}
